package com.clubs.coding.Controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.clubs.coding.Service.Impl.LearningServiceImpl;
import com.clubs.coding.common.ApiRestResponse;
import com.clubs.coding.model.dao.LearningMapper;
import com.clubs.coding.model.pojo.Learning;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/learning")
public class LearningController {
@Autowired
    LearningServiceImpl learningService;
@Autowired
    LearningMapper learningMapper;

//获取个人学习记录
@GetMapping("/userinfo")
    public ApiRestResponse userinfo(@RequestParam Integer pageNum,@RequestParam(value = "pageSize", required = false ,defaultValue = "10")Integer pageSize,String openid){
    PageInfo pageInfo = learningService.userinfo(pageNum, pageSize,openid);
    return ApiRestResponse.success(pageInfo);
    }
//训练记录
@GetMapping("/count")
public ApiRestResponse count(String openid){
    List<Learning> count = learningService.count(openid);
    return ApiRestResponse.success(count);
}
//成绩导出
@GetMapping("/export")
public void export(HttpServletResponse response,String openid) throws Exception {
    // 从数据库查询出所有的数据
    List<Learning> list = learningMapper.selectByOpenid(openid);
    // 通过工具类创建writer 写出到磁盘路径
//        ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");
    // 在内存操作，写出到浏览器
    ExcelWriter writer = ExcelUtil.getWriter(true);
    DataFormat dataFormat = writer.getWorkbook().createDataFormat();
//这里的日期格式化规则与SimpleDateFormat不一样,
//部分格式可参考org.apache.poi.ss.usermodel.BuiltinFormats
    short format = dataFormat.getFormat("yyyy-mm-dd hh:mm:ss");
    StyleSet styleSet = writer.getStyleSet();
    styleSet.getCellStyleForDate()
            .setDataFormat(format);
    //自定义标题别名
    writer.addHeaderAlias("childName", "用户名");
    writer.addHeaderAlias("report", "成绩");
    writer.addHeaderAlias("evaluate", "评价");
    writer.addHeaderAlias("projectName", "项目名称");
    writer.addHeaderAlias("nullItem", "评价时间");
    writer.addHeaderAlias("site", "地点");
    writer.addHeaderAlias("target", "目标");
    writer.addHeaderAlias("aboutTime", "上课时间");

    // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
    writer.setOnlyAlias(true);
    // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
    writer.write(list, true);

    // 设置浏览器响应的格式
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
    String fileName = URLEncoder.encode("用户信息", "UTF-8");
    response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

    ServletOutputStream out = response.getOutputStream();
    writer.flush(out, true);
    out.close();
    writer.close();

}

}
